"use strict";

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

var orders = require('./stroke-order-jian.json');

var strokeTable = require('./stroke-table.json');

var initOrderToWord = require('./orderToWord.js');

var _ = {}; // 工具方法

var arg = {
  letter: 'letter',
  shape: 'shape',
  count: 'count',
  name: 'name',
  detail: 'detail',
  array: 'array',
  order: 'order' // array 只是为了兼容 .stroke()

};

function main(cnchar) {
  if (cnchar.plugins.indexOf('order') !== -1) {
    return;
  }

  cnchar.plugins.push('order');
  var _old = cnchar._origin.stroke;
  _ = cnchar._;

  var _new = function _new() {
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    if (_.has(args, arg.order)) {
      // 使用order
      return _order.apply(void 0, args);
    }

    return _old.apply(void 0, args);
  };

  cnchar.stroke = _new;

  String.prototype.stroke = function () {
    for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
      args[_key2] = arguments[_key2];
    }

    return _new.apply(void 0, [this].concat(args));
  };

  cnchar.type.stroke = arg;
  cnchar._.order = true;
  cnchar._.orderWithLetters = orderWithLetters;

  if (cnchar._reinitStrokeOrder) {
    cnchar._reinitStrokeOrder();

    delete cnchar._reinitStrokeOrder;
  }

  initOrderToWord(cnchar);
}

function init(cnchar) {
  if ((typeof window === "undefined" ? "undefined" : _typeof(window)) === 'object' && window.CnChar) {
    main(window.CnChar);
  } else if (typeof cnchar !== 'undefined') {
    main(cnchar);
  }
}

function _order() {
  for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
    args[_key3] = arguments[_key3];
  }

  var strs = args[0].split(''); // 待处理的字符串数组

  args = args.splice(1);

  _.checkArgs('stroke', args); // 多音字参数参数将被忽略


  var res = [];

  for (var i = 0; i < strs.length; i++) {
    res[i] = orders[strs[i]]; // 字母版笔画表
  }

  return orderWithLetters(res, strs, args);
} // res:字母版笔画数组


function orderWithLetters(res, strs, args, igList) {
  igList = igList || [];

  if (_.has(args, arg.letter)) {
    return res;
  }

  for (var i = 0; i < res.length; i++) {
    if (igList.indexOf(i) === -1 && typeof res[i] === 'string') {
      res[i] = getStrokeSingle(strs[i], res[i], args);
    }
  }

  return res;
} // bug CnChar.stroke('長城','order','count')


function getStrokeSingle(str, order, args) {
  if (typeof order === 'undefined') {
    return str;
  }

  var isDetail = _.has(args, arg.detail);

  var name = arg.letter;

  if (!isDetail) {
    if (_.has(args, arg.shape)) {
      name = arg.shape;
    } else if (_.has(args, arg.name)) {
      name = arg.name;
    } else if (_.has(args, arg.count)) {
      name = arg.count;
    }
  } else {
    name = arg.detail;
  }

  if (name === arg.count) {
    return order.length;
  }

  if (name === arg.letter) {
    return order;
  }

  var arr = [];

  for (var i = 0; i < order.length; i++) {
    if (isDetail) {
      arr[i] = strokeTable[order[i]];
    } else {
      arr[i] = strokeTable[order[i]][name];
    }
  }

  return arr;
}

init();
module.exports = init;